package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.annotation.PreventDuplicateSubmit;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.CesForm;
import com.youlai.system.model.query.CesPageQuery;
import com.youlai.system.model.vo.CesPageVO;
import com.youlai.system.service.CesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "组件列表接口")
@RestController
@RequestMapping("/api/ces")
@RequiredArgsConstructor
public class CesController {
    private final CesService cesService;

    @Operation(summary = "组件分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<CesPageVO> getCesPage(
            @ParameterObject CesPageQuery queryParams
    ) {
        IPage<CesPageVO> result = cesService.getCesPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "删除组件", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    public Result deleteCess(
            @Parameter(description = "用户ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = cesService.deleteCess(ids);
        return Result.judge(result);
    }

    @Operation(summary = "新增组件", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreventDuplicateSubmit
    public Result saveDept(
            @Valid @RequestBody CesForm formData
    ) {
        boolean b = cesService.saveDict(formData);
        return Result.judge(b);
    }
}
